package com.cndw.kungfu.comm;

import java.util.List;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.domain.MonsterDeploy;
import com.cndw.kungfu.domain.Npc;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.vo.ScriptVO;

/**
 * 位置检查类
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-11-6 下午6:33:53
 */
public class PositionInspection {

	/**
	 * 检查玩家的移动是否合法
	 * 
	 * @param player
	 * @param x
	 * @param y
	 * @return
	 */
	public static boolean checkMove(Player player, int x, int y) {
		if (player.getLevel() <= 42 && !player.stateContains(Constants.playerState256_dart)) {//42级别以下玩家不检查
			return true;
		}
		int distance = Math.abs(x - player.getX());
		if (distance <= 100) {
			return true;
		}
		if (player.getTz() == 0) {
			// LogBase.loger.debug("speed err " + player.getPlayerName());
			// return false;
		}

		int speed = distance * 1000 / (int) (TimeProcesser.currentTimeMillis() - player.getTz() + 1);
		if (speed >= 400) {
			// LogBase.loger.debug("speed fast " + speed + " acc:" +
			// player.getAccountName());
			return false;
		}

		return true;
	}

	/**
	 * 检查是否在npc附近
	 * 
	 * @param player
	 * @param npc
	 * @return
	 */
	public static boolean checkNpc(Player player, Npc npc) {
		if (player == null || npc == null) {
			return false;
		}
		if (player.getSceneId() != npc.getSceneId()) {
			return false;
		}

		return checkComm(player, npc.getX(), npc.getY(), 300, 500);
	}

	public static boolean checkNpc(Player player, int npcId) {
		if (npcId == 44) {// 商会跑环任务,不检查距离
			return true;
		}
		return checkNpcMain(player, npcId);
	}
	public static boolean checkNpcMain(Player player,int npcId){
		Npc npc = Context.getSceneServer().getNpcById(npcId);
		return checkNpc(player, npc);
	}

	/**
	 * 检查是否在怪物附近
	 * 
	 * @param player
	 * @param monsterDeploy
	 * @return
	 */
	public static boolean checkMonster(Player player, MonsterDeploy deploy) {
		if (player == null || deploy == null) {
			return false;
		}
		if (player.getSceneId() != deploy.getSceneId()) {
			return false;
		}
		return checkComm(player, deploy.getX(), deploy.getY(), 600, 600);
	}

	/**
	 * 检查是否在玩家附近
	 * 
	 * @param player
	 * @param target
	 * @return
	 */
	public static boolean checkPlayer(Player player, Player target) {
		if (player == null || target == null) {
			return false;
		}
		if (player.getSceneId() != target.getSceneId()) {
			return false;
		}
		return checkComm(player, target.getX(), target.getY(), 400);
	}

	public static boolean checkComm(Player player, int targetX, int targetY, int Offset) {
		int offx = player.getX() - targetX;
		int offy = player.getY() - targetY;
		if (Math.abs(offx) > Offset || Math.abs(offy) > Offset) {
			return false;
		}
		return true;
	}

	public static boolean checkComm(Player player, int targetX, int targetY, int Offset, int offSexY) {
		int offx = player.getX() - targetX;
		int offy = player.getY() - targetY;
		if (Math.abs(offx) > Offset || Math.abs(offy) > offSexY) {
			return false;
		}
		return true;
	}

	/**
	 * 检查是否在仓库附近
	 * 
	 * @param player
	 * @return
	 */
	public static boolean checkHoldPosition(Player player) {
		List<Npc> npcs = Context.getSceneServer().getNpcBySceneId(player.getSceneId());
		if (npcs == null) {
			return false;
		}
		Npc holdNpc = null;
		for (Npc npc : npcs) {
			if (npc.getScripts() != null) {
				for (ScriptVO e : npc.getScripts()) {
					if (e.id == 3) {
						holdNpc = npc;
					}
				}
			}
		}
		if (holdNpc == null) {
			return false;
		}
		return checkNpc(player, holdNpc);
	}

	/**
	 * 检查是否可以进入目标场景
	 * 
	 * @param player
	 * @param targetScene
	 * @return
	 */
	public static boolean checkScene(Player player, Scene targetScene) {
		if (player.getSceneId() == 0 || player.getSceneId() == targetScene.getId()) {
			return true;
		}
		Scene currentScene = Context.getSceneServer().getSceneById(player.getSceneId());
		if (currentScene == null) {
			return true;
		}
		if (!player.stateContains(Constants.playerState256_dart)) {
			//主城 塞外 秘籍 可以直接传送
			if (targetScene.isCityMain() || targetScene.isCityUnionWar() || targetScene.isCityFam()) {
				return true;
			}
		}
		if (currentScene.isCityMain() && (targetScene.isCityWall())) {
			return true;// 允许从主城传到副本 塞外. 这些直接传送的检测做在进入函数里面
		} else if (targetScene.getLeftScene() == 0) {// 没有左门的允许直接进入
			// return true;
		}
	
		if ((currentScene.isCityMain() || currentScene.isCityField()) && targetScene.isCityUndercity()) {
			return true;// 允许主城或野外直接进入副本
		}

		int targetId = targetScene.getId();
		if (player.getTs() == targetId) {// 使用了奇门遁甲
			return true;
		} else if (player.getDataInfo().getLastSceneId() == targetId) {
			return true;
		}
		Team team = player.getTeam();
		if (team != null && team.isMaster(player.getPlayerId()) == false) {
			Player masterPlayer = Context.getPlayerServer().getByPlayerId(team.getMaster().getUniqueId());
			if (masterPlayer.getSceneId() == targetId || masterPlayer.getTs() == targetId) {
				return true;// 队长所在的场景或者使用了奇门遁甲.允许前往
			}
		}
		int x = 0;
		int y = 0;

		if (targetId == currentScene.getRightScene()) {
			x = currentScene.getBirthPoint()[2];
			y = currentScene.getBirthPoint()[3];
		} else if (targetId == currentScene.getLeftScene()) {
			x = currentScene.getBirthPoint()[0];
			y = currentScene.getBirthPoint()[1];
		} else if (targetId == currentScene.getUpScene()) {
			x = currentScene.getBirthPoint()[4];
			y = currentScene.getBirthPoint()[5];
		} else if (targetId == currentScene.getDownScene()) {
			x = currentScene.getBirthPoint()[6];
			y = currentScene.getBirthPoint()[7];
		} else {
			return false;
		}
		return checkComm(player, x, y, 300);
	}

	public static boolean checkMainScene(Player player) {
		Scene scene = Context.getSceneServer().getSceneById(player.getSceneId());
		return scene != null && scene.isCityMain();
	}

}
